package com.lhw.ai.api;

import com.lhw.ai.service.BailianService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

/**
 * 文件相关API接口
 */
@Tag(name = "文件管理接口", description = "提供文件上传和管理相关接口")
@RestController
@RequestMapping("/api/file")
public class FileApi {

    @Resource
    private BailianService bailianService;

    /**
     * 导入数据到百炼
     */
    @Operation(summary = "导入文件数据", description = "将指定路径的文件数据导入到百炼平台")
    @ApiResponse(responseCode = "200", description = "成功导入文件数据", 
        content = @Content(mediaType = "application/json", 
        schema = @Schema(implementation = String.class)))
    @GetMapping("/import")
    public String importData(
            @Parameter(description = "文件路径", required = true) 
            @RequestParam(value = "filePath") String filePath) {
        bailianService.uploadFile(filePath);
        return "导入成功";
    }

    /**
     * 查询百炼数据RAG
     */
    @Operation(summary = "查询RAG数据", description = "从百炼平台查询RAG相关数据")
    @ApiResponse(responseCode = "200", description = "成功返回查询结果", 
        content = @Content(mediaType = "application/json", 
        schema = @Schema(implementation = String.class)))
    @GetMapping("/query")
    public String queryData(
            @Parameter(description = "查询数据内容", required = true) 
            @RequestParam(value = "queryData") String queryData) {
        return bailianService.queryRag(queryData);
    }

    /**
     * 文件上传接口
     */
    @Operation(summary = "上传文件", description = "上传文件到服务器指定目录")
    @ApiResponse(responseCode = "200", description = "成功上传文件", 
        content = @Content(mediaType = "application/json", 
        schema = @Schema(implementation = String.class)))
    @ApiResponse(responseCode = "500", description = "文件上传失败", 
        content = @Content(mediaType = "application/json", 
        schema = @Schema(implementation = String.class)))
    @PostMapping("/upload")
    public String uploadFile(
            @Parameter(description = "要上传的文件", required = true) 
            @RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return "文件为空";
        }

        // 创建上传目录
        File uploadDir = new File("uploads/");
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }

        // 生成唯一文件名
        String originalFilename = file.getOriginalFilename();
        String fileExtension = "";
        if (originalFilename != null && originalFilename.contains(".")) {
            fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String uniqueFilename = UUID.randomUUID().toString() + fileExtension;

        // 保存文件
        byte[] bytes = file.getBytes();
        Path path = Paths.get("uploads/" + uniqueFilename);
        Files.write(path, bytes);

        return "文件上传成功: " + uniqueFilename;
    }
}